package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

//@RestController
//@RequestMapping("/system/draw-card")
public class DrawCardController {

	// 名片输出目录（从配置文件读取）
	@Value("${ruoyi.outputDir}")
	private String outputDir;

	// 模板地址
	@Value("${ruoyi.templatePath}")
	private String templatePath;

	@PostMapping("/generate")
	@Anonymous
	public ResponseEntity<?> generateBusinessCard(@RequestBody Map<String, String> requestData) {
		try {
			// 从请求数据中获取各个字段
			String avatar = requestData.getOrDefault("avatar", "未知");
			String name = requestData.getOrDefault("name", "未知");
			String title = requestData.getOrDefault("position", "职位未填");
			String company = requestData.getOrDefault("company", "公司未填");
			String phone = requestData.getOrDefault("phone", "手机号未填");
			String email = requestData.getOrDefault("email", "邮箱未填");
			String address = requestData.getOrDefault("address", "地址未填");

			// 确保输出目录存在
			File outputDirectory = new File(outputDir);
			if (!outputDirectory.exists()) {
				outputDirectory.mkdirs();
			}

			// 加载模板图片
			BufferedImage templateImage = ImageIO.read(new File(templatePath));

			// 创建 Graphics 对象
			Graphics2D graphics = templateImage.createGraphics();
			graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

			// 绘制内容到图片
			BufferedImage avatarImage = null;
			try {
				avatarImage = ImageIO.read(new URL(avatar));  // 读取头像图片
			} catch (IOException e) {
				e.printStackTrace();
				// 处理头像加载失败的情况
			}

			// 获取模板宽度（假设模板宽度是 600）
			int templateWidth = templateImage.getWidth();

			// 假设头像宽度是 200
			int avatarWidth = 200;

			// 计算头像的 X 坐标，使其位于右边
			int avatarX = templateWidth - avatarWidth - 50;  // 50 是距离右边的偏移量
			int avatarY = 50;  // Y 坐标不变

			if (avatarImage != null) {
				graphics.drawImage(avatarImage, avatarX, avatarY, avatarWidth, avatarWidth, null);
			}

			graphics.setFont(new Font("微软雅黑", Font.BOLD, 30));
			graphics.setColor(Color.BLACK);
			graphics.drawString(name, 100, 140);
			graphics.setFont(new Font("微软雅黑", Font.PLAIN, 20));
			graphics.drawString(title, 420, 230);//职务
			graphics.drawString(company, 100, 230);//公司
			graphics.drawString(phone, 420, 140);
			graphics.drawString(email, 420, 320);
			graphics.drawString(address, 100, 320);

			// 释放资源
			graphics.dispose();

			// 保存图片到指定目录
			String outputFilePath = name + "_card.png";
			ImageIO.write(templateImage, "png", new File(outputDir + outputFilePath));

			// 构建返回结果
			Map<String, Object> response = new HashMap<>();
			response.put("message", "名片生成成功");
			response.put("fileName", outputFilePath);

			return ResponseEntity.ok(response);

		} catch (IOException e) {
			e.printStackTrace();
			Map<String, Object> errorResponse = new HashMap<>();
			errorResponse.put("message", "名片生成失败");
			errorResponse.put("error", e.getMessage());
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
		}
	}

}
